package cn.edu.leetcode.数组操作;

import java.util.ArrayList;
import java.util.List;

public class 汇总区间 {

    public static void main(String[] args) {
        int[] nums = {0, 1, 2, 4, 5, 7};
        List<String> strings = summaryRanges(nums);
        System.out.println(strings);
    }

    public static List<String> summaryRanges(int[] nums) {
        ArrayList<String> strings = new ArrayList<>();
        if (nums == null || nums.length == 0){
            return strings;
        }
        int b = nums[0], e = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (e + 1 == nums[i]) {
                e++;
            } else {
                if (b == e) {
                    strings.add(b + "");
                }else {
                    strings.add(b + "->" + e);
                }

                b = nums[i];
                e = nums[i];
            }
        }

        if (b == e) {
            strings.add(b + "");
        }else {
            strings.add(b + "->" + e);
        }

        return strings;
    }
}
